/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {

    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode) {
        if (pNode->right)
        {
            TreeLinkNode* cur = pNode->right;
            TreeLinkNode* prev = nullptr;
            while (cur)
            {
                prev = cur;
                cur = cur->left;
            }
            return prev;
        }
        TreeLinkNode* root = pNode->next;
        while (root)
        {
            if (root->left == pNode)
            {
                return root;
            }
            pNode = root;
            root = root->next;

        }
        return nullptr;
    }
};